<?php
/**
 * @category 	MemberController.php
 * @author 		Sung Hoon, Cho (akdddnet@gmail.com)
 * @since		2011. 5. 2.
 * @license		BSD License
 * @version		0.1
 */

global $CFG;
require_once $CFG->FW_DIR."/mvc/Controller.php";
require_once $CFG->CLASSES_DIR."/member/MemberEB.php";
require_once $CFG->WEBINF_DIR."/utility/MemberUtil.php";

class MemberController extends Controller {
	 
	function __construct() {
		parent::__construct(get_class());	// 꼭 부모의 construct를 실행 해줘야됩니다.
	}

	function login() {
		$this->tpl->define(array("main"=>"login.html"));
	}
	
	
	function doLogin() {
		$param = new Parameter(false);
		
		$mid = $param->getParameter("uid");
		$mpwd = $param->getParameter("upwd");
		
		// validate 검사
		$validate = new Validate();
		$validate->add(array("required", "userid"), $mid, "사용자 아이디");
		$validate->add("required", $mpwd, "사용자 비밀번호");
		
		if($validate->validate()) {
			$meb = new MemberEB();
			$meb->m_id = $mid;
			$meb->m_password = $mpwd;
			
			$dto = new DTO();
			$dto->eb = $meb;
			
			if($member = $this->dao->doLogin($dto)) {
				$_SESSION["owner"] = $member;

				global $CFG;
				Javascript::move($CFG->PATH->WWWADMIN."/dash", true);
			} else {
				Javascript::getAlert("아이디 또는 비밀번호 오류입니다.");
			}
		} else {
			Javascript::getAlert($validate->getErrorMessage());	
		}
	}
	
	
	function doLogout() {
		unset($_SESSION["owner"]);
		
		global $CFG;
		Javascript::getAlertMove("로그아웃 되었습니다.", $CFG->PATH->WWWADMIN."/member/login", true);
	}
	
	function index() {
		$this->page->title = '회원관리';
		
		$dto = $this->dao->index(new DTO());
		
		$list = $dto->list;
		if($list) {
			foreach($list as $m) {
				$m->auth = ($m->m_auth) ? "인증취소" : "인증승인";
			}
		}
		
		$this->tpl->assign(array(
			"list"=>$list
			,"script"=>Javascript::getjQueryValidateFile()
		));
	}
	
	
	function view($seq) {
		if(is_numeric($seq)) {
			$dto = new DTO();
			$eb = new MemberEB();
			$eb->m_seq = $seq;
			$dto->eb = $eb;
			
			$result = $this->dao->view($dto)->obj;
			
			if(!Common::isNullOrEmpty($result)) {
				
				$result->auth = getAuth($result->m_auth);
				$result->sendemail = getSendEmail($result->m_sendemail);
				$result->sign = getSign($result->m_sign);
				
				$result->mdate = Common::getNullToString($result->m_mdate);
				
				global $CFG;
				$result->reloadurl = $CFG->wwwroot."/admin/member/view/".$result->m_seq;
				 
				$this->tpl->assign(array(
					"result"=>$result
				));	
			} else {
				Javascript::getAlert("존재하지 않는 회원 입니다.");	
			}
		} else {
			Javascript::getAlert("잘못된 값이 전달되었습니다.");
		}
	}
	
	function add() {
		$this->page->class = 'member_add';
		$this->page->title = '회원추가';
		
		$script = array();
		array_push($script, 'tiny_mce/jquery.tinymce.js');
		
		$this->tpl->assign(array(
				"script"=>$script
		));
	}
	
	
	function doAdd() {
		
		$param = new Parameter(false);
		
		$validate = new Validate();
		$validate->add(array("required", "userid"), $param->getParameter("mem_id2"), "아이디");
		$validate->add(array("required", "password"), $param->getParameter("mem_pwd"), "비밀번호");
		$validate->add(array("required", "password", "match"=>$param->getParameter("mem_pwd")), $param->getParameter("mem_pwd_confirm"), "비밀번호 확인");
		$validate->add("required", $param->getParameter("mem_name"), "이름");
		$validate->add(array("required", "email"), $param->getParameter("mem_email"), "이메일");
		
		
		if($validate->validate()) {
			$dto = new DTO();
			
			$eb = new MemberEB();
			$eb->m_id = $param->getParameter("mem_id2");
			$eb->m_password = $param->getParameter("mem_pwd_confirm");
			$eb->m_name = $param->getParameter("mem_name");
			$eb->m_email = $param->getParameter("mem_email");
			$eb->m_sendemail = $param->getParameter("mem_sendemail");
			$eb->m_auth = $param->getParameter("mem_auth");
			$eb->m_sign = $param->getParameter("mem_sign");
			
			$dto->eb = $eb;
			
			$result = $this->dao->doAdd($dto)->rc;
			if($result > 0) {
				global $CFG;
				
				$message = "정상적으로 추가 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/member/";
				
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
			} else {
				Javascript::getAlertWithAjax("추가시 문제가 발생하였습니다.");
			}
		} else {
			Javascript::getAlertWithAjax($validate->getErrorMessage());
		}
	}
	
	
	function modify($seq) {
		// seq값은 숫자만 전달 받아야됨.
		if(is_numeric($seq)) {
			$dto = new DTO();
			$eb = new MemberEB();
			$eb->m_seq = $seq;
			$dto->eb = $eb;
			
			$result = $this->dao->view($dto)->obj;
			
			if(!Common::isNullOrEmpty($result)) {
				$this->page->class = 'member_add';
				$this->page->title = $result->m_name.' 회원 수정';
				
				
				$script = array();
				array_push($script, 'tiny_mce/jquery.tinymce.js');
				
				$this->tpl->assign(array(
					"result"=>$result
					,"script"=>$script
					
				));	
			} else {
				Javascript::getAlert("존재하지 않는 회원 입니다.");	
			}
		} else {
			Javascript::getAlert("잘못된 값이 전달되었습니다.");
		}
	}
	
	
	function doModify() {
		$param = new Parameter(false);
		
		$validate = new Validate();
		$validate->add(array("required", "number"), $param->getParameter("mem_seq"), "고유번호");
		$validate->add("required", $param->getParameter("mem_name"), "이름");
		$validate->add(array("required", "email"), $param->getParameter("mem_email"), "이메일");
		
		
		if($validate->validate()) {
			$dto = new DTO();
			
			$eb = new MemberEB();
			$eb->m_seq = $param->getParameter("mem_seq");
			$eb->m_name = $param->getParameter("mem_name");
			$eb->m_email = $param->getParameter("mem_email");
			$eb->m_sendemail = $param->getParameter("mem_sendemail");
			$eb->m_auth = $param->getParameter("mem_auth");
			$eb->m_sign = $param->getParameter("mem_sign");
			
			$dto->eb = $eb;
			
			$result = $this->dao->doUpdate($dto)->rc;
			if($result > 0) {
			global $CFG;
				
				$message = "정상적으로 수정 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/member/view/".$eb->m_seq;
				
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
				
			} else {
				Javascript::getAlertWithAjax("수정시 문제가 발생하였습니다.");
			}
		} else {
			Javascript::getAlertWithAjax($validate->getErrorMessage());
		}
	}
	
	
	function doDelete($seq) {
		if(is_numeric($seq)) {
			$dto = new DTO();
			$eb = new MemberEB();
			$eb->m_seq = $seq;
			
			$dto->eb = $eb;
			
			$result = $this->dao->doDelete($dto)->rc;
			if($result > 0) {
				global $CFG;
				
				$message = "삭제 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/member/";
				
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
				
			} else {
				Javascript::getAlertWithAjax("삭제시 문제가 발생하였습니다.");
			}
		} else {
			Javascript::getAlertWithAjax("잘못된 값이 전달되었습니다.");
		}
	}
	
	
	function doAuth($seq) {
		return self::_doAuth($seq, 1);
	}
	
	
	function doNotAuth($seq) {
		return self::_doAuth($seq, 0);
	}
	
	private function _doAuth($seq, $auth) {
		if(Common::isAjax()) {
			if(is_numeric($seq)) {
				$dto = new DTO();
				
				$eb = new MemberEB();
				$eb->m_seq = $seq;
				$eb->m_auth = $auth;
				$dto->eb = $eb;
				
				$result = $this->dao->doAuth($dto)->rc;
				
				if($result > 0) {
					Javascript::printJSON(array("code"=>Javascript::getSuccessCode(), "msg"=>"정상적으로 처리 되었습니다."));
				} else {
					Javascript::printJSON(array("code"=>Javascript::getFailureCode(), "msg"=>"인증 승인/취소시 문제가 발생하였습니다."));
				}
			} else {
				Javascript::printJSON(array("code"=>Javascript::getFailureCode(), "msg"=>"잘못된 값이 전달되었습니다."));
			}
		} else {
			Javascript::getAlert("ajax 통신 전용입니다.");
		}
	}
	
	
	function passwordChange($seq) {
		
		//$this->tpl->define(array("main"=>"member/passwordChange.html"));
		$this->tpl->assign(array("seq"=>$seq));
	}
	
	function doPasswordChange() {
		if(Common::isAjax()) {
			$param = new Parameter(false);
			
			$validate = new Validate();
			$validate->add("required", $param->getParameter("mem_seq"), "고유번호");
			$validate->add(array("required", "password"), $param->getParameter("mem_pwd"), "비밀번호");
			$validate->add(array("required", "password"), $param->getParameter("mem_pwd_change"), "변경 비밀번호");
			$validate->add(array("required", "password", "match"=>$param->getParameter("mem_pwd_change")), $param->getParameter("mem_pwd_change_confirm"), "변경 비밀번호 확인");
			
			if($validate->validate()) {
				// 비밀번호 검사
				$dto = new DTO();
				$eb = new MemberEB();
				$eb->m_seq = $param->getParameter("mem_seq"); 
				$eb->m_password = $param->getParameter("mem_pwd");
				$dto->eb = $eb;
				
				$result = $this->dao->getPasswordCheck($dto)->rc;
				if($result > 0) {
					$eb->m_password = $param->getParameter("mem_pwd_change_confirm");
					$dto->eb = $eb;
					
					$result = $this->dao->doPasswordChange($dto)->rc;
					
					if($result > 0) {
						Javascript::printJSON(array("code"=>Javascript::getSuccessCode(), "msg"=>"정상적으로 처리 되었습니다."));
					} else {
						Javascript::printJSON(array("code"=>Javascript::getFailureCode(), "msg"=>"비밀번호 변경시 문제가 발생하였습니다."));
					}	
				} else {
					Javascript::printJSON(array("code"=>Javascript::getFailureCode(), "msg"=>"비밀번호가 일치하지 않습니다."));	
				}
			} else {
				Javascript::printJSON(array("code"=>Javascript::getFailureCode(), "msg"=>$validate->getErrorMessage()));
			}
			
		} else {
			Javascript::getAlert("ajax 통신 전용입니다.");
		}
	}
}

